package src;

import pjwstk.jps.ast.IExpression;
import pjwstk.jps.datastore.SBAStore;
import pjwstk.jps.interpreter.envs.ENVS;
import pjwstk.jps.interpreter.envs.Interpreter;
import pjwstk.jps.interpreter.qres.QResStack;
import pjwstk.jps.parser.SBQLParser;
import pjwstk.jps.result.IAbstractQueryResult;
import java.io.*;

public class Main7 {
    @SuppressWarnings("unused")
    public static void main(String[] args) throws Exception {
        System.out.println("Pr7 - Lekser + Parser");

        ENVS envs1 = new ENVS();           // envs1||
        QResStack qres1 = new QResStack(); // qres1||
        SBAStore store1 = new SBAStore();  // store1||

        store1.loadXML(".\\s9111_s10304_pr7\\res\\data.xml");
        envs1.init(store1.getEntryOID(), store1);
        store1.PrintObjects(store1.retrieve(store1.getEntryOID()));
        //System.out.println(store1.getString());

        //Interpreter interpreter = new Interpreter(new SBAStore());
        Interpreter interpreter = new Interpreter(store1);
        
        try {
            //FileInputStream fstream = new FileInputStream(".\\s9111_s10304_pr7\\res\\examples.txt");
            FileInputStream fstream = new FileInputStream(".\\s9111_s10304_pr7\\res\\tests.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
             
            String strLine;
            int i = 1;
            while ((strLine = br.readLine()) != null) {
                SBQLParser parser = new SBQLParser(strLine);
                parser.user_init();
                parser.parse();
                    
                IExpression expression = parser.RESULT;
                    
                IAbstractQueryResult queryResult = interpreter.eval(expression);
                System.out.println("ex" + i + ": " + strLine + " = " + queryResult.toString());
                i++;
            }
            in.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}